package flc.ast.activity;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;
import com.stark.translator.util.LanCodeUtil;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTranslateBinding;
import flc.ast.dialog.LanguageDialog;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.other.LanCode;
import stark.common.other.ReqManager;
import stark.common.other.bean.TranslateRet;

public class TranslateActivity extends BaseAc<ActivityTranslateBinding> {

    private LanCode mCurSelLanCode;
    private static final int GEN_LAN_CODE_COUNT = 3;
    private BasePopupView languagePopup;
    private LanguageDialog languageDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_translate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivTranslateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llResultLan.setOnClickListener(this);
        mDataBinding.ivTranslate.setOnClickListener(this);
        mDataBinding.ivCopy.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llResultLan://切换语言
                showLanguageDialog();
                break;
            case R.id.ivTranslate://翻译
                translate();
                break;
            case R.id.ivCopy://复制
                if (TextUtils.isEmpty(mDataBinding.tvTransResult.getText().toString().trim())) {
                    ToastUtils.showShort(R.string.no_content_copy_tips);
                } else {
                    ClipboardUtils.copyText(mDataBinding.tvTransResult.getText().toString().trim());
                    ToastUtils.showShort(getString(R.string.copy_success_tips));
                }
                break;
        }
    }

    @Override
    protected void initData() {
        List<LanCode> lanCodeList = LanCodeUtil.extractFromGeneralLanCodeList(GEN_LAN_CODE_COUNT);
        if (lanCodeList != null && lanCodeList.size() > 0) {
            mCurSelLanCode = lanCodeList.get(0);
            mDataBinding.tvResultLan.setText(mCurSelLanCode.getName());
        }
    }

    private void translate() {
        String content = mDataBinding.etInput.getText().toString();
        if (TextUtils.isEmpty(content) || mCurSelLanCode == null) {
            return;
        }

        showDialog(getString(R.string.translating));
        ReqManager.getInstance().translate(this, content, LanCode.AUTO, mCurSelLanCode, new IReqRetCallback<TranslateRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable TranslateRet result) {
                dismissDialog();
                if (result != null) {
                    //将本次翻译成功的文本保存起来
                    mDataBinding.tvTransResult.setText(result.getResult());
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }

    private void showLanguageDialog() {
        if (languageDialog == null) languageDialog = new LanguageDialog(mContext);
        languagePopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.llResultLan).hasShadowBg(false) // 去掉半透明背景
                .asCustom(languageDialog);
        languageDialog.setListener(new LanguageDialog.Ilistener() {
            @Override
            public void clickSelLan(LanCode lanCode) {
                mCurSelLanCode = lanCode;
                mDataBinding.tvResultLan.setText(lanCode.getName());
                translate();
            }
        });
        languagePopup.show();
    }

}